define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'weichat_coupon_order/index' + location.search,
                    add_url: 'weichat_coupon_order/add',
                    edit_url: 'weichat_coupon_order/edit',
                    del_url: 'weichat_coupon_order/del',
                    multi_url: 'weichat_coupon_order/multi',
                    table: 'weichat_coupon_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: '订单id',operate:false},
                        {field: 'openid', title: 'openid', operate:'LIKE'},
                        {field: 'mobile', title: '手机号',operate: 'LIKE'},
                        {field: 'brand.username', title: '所属商户', operate: 'LIKE'},
                        {field: 'city.name', title: '地区', operate: 'LIKE'},
                        {field: 'good.name', title: '商品名称',operate: 'LIKE'},
                        {field: 'batch_no', title: '批次号', operate:'false'},
                        {field: 'coupon_id', title: '代金券id', operate:'false'},
                        {field: 'transaction_id', title: '微信支付订单号', operate:'false'},
                        {field: 'consume_mchid', title: '核销商户号', operate:'false'},
                        {field: 'status', title: '状态',searchList:{"1":'已领取',"2":'已核销',"5":'已过期',"6":'已激活'},formatter: Table.api.formatter.normal},
                        {field: 'get_time', title: '领取时间', operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: '过期时间', operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'activate_time', title: '激活时间', operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'up_time', title: '核销时间', operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'account', title: '领取平台'},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
$(document).on("click", ".open-callback", function(){
    $.post({
        url: 'weichat_coupon_order/openCallback',
        type: 'POST',
        dataType: 'json',
        data: {type: 'true'},
        cache: false,
        success: function (ret) {
            if(ret.code == 200){
                Toastr.success('设置成功');
            }else{
                Toastr.error('设置失败');
            }
        }, error: function () {
            Toastr.error(__('Network error'));
        }
    });
});
$(document).on("click", ".close-callback", function(){
    $.post({
        url: 'weichat_coupon_order/openCallback',
        type: 'POST',
        dataType: 'json',
        data: {type: 'false'},
        cache: false,
        success: function (ret) {
            if(ret.code == 200){
                Toastr.success('设置成功');
            }else{
                Toastr.error('设置失败');
            }
        }, error: function () {
            Toastr.error(__('Network error'));
        }
    });
});